import QtQuick 2.9
import QtQuick.Controls 2.2
import QtTest 1.0

import InputMethod 1.0

Item {
    id: virtualKeyboardRoot
    focus: false
    property int keySpacing: 8
    property int keyButtonWidth: (width - keySpacing*11) / 10
    property int keyButtonHeight: (keyboardHeight - keySpacing * 4) / 4
    property int keyTextFontSize: 30
    property int shiftKeyTextFontSize: 15
    property int keyboardHeight: height / 2
    property string color: "AliceBlue"
    property int defaultRootY: 0

    property bool active: false
    property bool capsLock: false
    property bool shift: false
    property bool raise: false
    property bool chinese: true
    property var root
    property var window

    Rectangle {
        id: coverItem
        anchors.left: virtualKeyboardRec.left
        anchors.right: virtualKeyboardRec.right
        height: 0
        anchors.bottom: virtualKeyboardRec.top
        color: virtualKeyboardRec.color
        MouseArea {
            anchors.fill: parent
        }
    }

    Rectangle {
        id: virtualKeyboardRec
        anchors.left: parent.left
        anchors.right: parent.right
        height: keyboardHeight
        y : parent.height
        color: parent.color
        MouseArea {
            anchors.fill: parent
        }
        Column {
            id: generalKeyboard
            anchors.centerIn: parent
            spacing: keySpacing
            Row {
                id: row1
                anchors.horizontalCenter: parent.horizontalCenter
                spacing: keySpacing
                Repeater {
                    model: ListModel {
                        ListElement {key_text: "Q"; key_value : Qt.Key_Q; shift_key_text: "1"; shift_key_value: Qt.Key_1}
                        ListElement {key_text: "W"; key_value : Qt.Key_W; shift_key_text: "2"; shift_key_value: Qt.Key_2}
                        ListElement {key_text: "E"; key_value : Qt.Key_E; shift_key_text: "3"; shift_key_value: Qt.Key_3}
                        ListElement {key_text: "R"; key_value : Qt.Key_R; shift_key_text: "4"; shift_key_value: Qt.Key_4}
                        ListElement {key_text: "T"; key_value : Qt.Key_T; shift_key_text: "5"; shift_key_value: Qt.Key_5}
                        ListElement {key_text: "Y"; key_value : Qt.Key_Y; shift_key_text: "6"; shift_key_value: Qt.Key_6}
                        ListElement {key_text: "U"; key_value : Qt.Key_U; shift_key_text: "7"; shift_key_value: Qt.Key_7}
                        ListElement {key_text: "I"; key_value : Qt.Key_I; shift_key_text: "8"; shift_key_value: Qt.Key_8}
                        ListElement {key_text: "O"; key_value : Qt.Key_O; shift_key_text: "9"; shift_key_value: Qt.Key_9}
                        ListElement {key_text: "P"; key_value : Qt.Key_P; shift_key_text: "0"; shift_key_value: Qt.Key_0}
                    }
                    delegate: keyButton
                }
            }
            Row {
                id: row2
                anchors.horizontalCenter: parent.horizontalCenter
                spacing: keySpacing
                Repeater {
                    model: ListModel {
                        ListElement {key_text: "A"; key_value : Qt.Key_A; shift_key_text: "~"; shift_key_value: Qt.Key_AsciiTilde}
                        ListElement {key_text: "S"; key_value : Qt.Key_S; shift_key_text: "!"; shift_key_value: Qt.Key_Exclam}
                        ListElement {key_text: "D"; key_value : Qt.Key_D; shift_key_text: "@"; shift_key_value: Qt.Key_At}
                        ListElement {key_text: "F"; key_value : Qt.Key_F; shift_key_text: "#"; shift_key_value: Qt.Key_NumberSign}
                        ListElement {key_text: "G"; key_value : Qt.Key_G; shift_key_text: "%"; shift_key_value: Qt.Key_Percent}
                        ListElement {key_text: "H"; key_value : Qt.Key_H; shift_key_text: "+"; shift_key_value: Qt.Key_Plus}
                        ListElement {key_text: "J"; key_value : Qt.Key_J; shift_key_text: "*"; shift_key_value: Qt.Key_multiply}
                        ListElement {key_text: "K"; key_value : Qt.Key_K; shift_key_text: "("; shift_key_value: Qt.Key_ParenLeft}
                        ListElement {key_text: "L"; key_value : Qt.Key_L; shift_key_text: ")"; shift_key_value: Qt.Key_ParenRight}
                    }
                    delegate: keyButton
                }
            }
            Row {
                id: row3
                anchors.horizontalCenter: parent.horizontalCenter
                spacing: keySpacing
                ItemDelegate {
                    id: capsLockButton
                    width: keyButtonWidth
                    height: keyButtonHeight
                    Image {
                        id: capsLockImage
                        visible: false
                        anchors.centerIn: parent
                        height: keyButtonHeight
                        width: height
                        source: capsLock ? "icons/capslock_on.png" : "icons/capslock_off.png"
                    }
                    Text {
                        id: apostropheText
                        anchors.centerIn: parent
                        font.pixelSize: keyTextFontSize
                        text: "分"
                    }
                    property int keyValue: Qt.Key_Apostrophe
                    states: [
                        State {
                            name: "capsLock"
                            when: !chinese
                            PropertyChanges {
                                target: capsLockImage
                                visible: true
                            }
                            PropertyChanges {
                                target: apostropheText
                                visible: false
                            }
                            PropertyChanges {
                                target: capsLockButton
                                keyValue: Qt.Key_CapsLock
                            }
                        }
                    ]
                    onClicked: {
                        keySimulator.keyClick(keyValue, Qt.NoModifier, -1)
                        if (keyValue == Qt.Key_CapsLock) {
                            capsLock = !capsLock
                        }
                    }
                }
                Repeater {
                    id: row3Rep
                    model: ListModel {
                        ListElement {key_text: "Z"; key_value : Qt.Key_Z; shift_key_text: "\""; shift_key_value: Qt.Key_QuoteDbl}
                        ListElement {key_text: "X"; key_value : Qt.Key_X; shift_key_text: "/"; shift_key_value: Qt.Key_Slash}
                        ListElement {key_text: "C"; key_value : Qt.Key_C; shift_key_text: "-"; shift_key_value: Qt.Key_Minus}
                        ListElement {key_text: "V"; key_value : Qt.Key_V; shift_key_text: "_"; shift_key_value: Qt.Key_Underscore}
                        ListElement {key_text: "B"; key_value : Qt.Key_B; shift_key_text: ":"; shift_key_value: Qt.Key_Colon}
                        ListElement {key_text: "N"; key_value : Qt.Key_N; shift_key_text: ";"; shift_key_value: Qt.Key_Semicolon}
                        ListElement {key_text: "M"; key_value : Qt.Key_M; shift_key_text: "?"; shift_key_value: Qt.Key_Question}
                    }
                    delegate: keyButton
                }
                Loader {
                    id: normalBackspaceButton
                    width: keyButtonWidth * 2
                    height: keyButtonHeight
                    sourceComponent: backspaceButton
                }
            }
            Row {
                id: row4
                anchors.horizontalCenter: parent.horizontalCenter
                spacing: keySpacing
                ItemDelegate {
                    id: symbolButton
                    Text {
                        anchors.centerIn: parent
                        text: "123"
                        font.pixelSize: keyTextFontSize
                    }
                    width: keyButtonWidth * 2
                    height: keyButtonHeight
                    onClicked: {
                        shift = !shift
                    }
                }
                //                Loader {
                //                    sourceComponent: keyButton
                //                    onLoaded: {
                //                        item.general_text = "分"
                //                        item.general_value = Qt.Key_Apostrophe
                //                        item.shift_text = "'"
                //                        item.shift_value = Qt.Key_Apostrophe
                //                    }
                //                }
                ItemDelegate {
                    id: languageButton
                    width: keyButtonWidth
                    height: keyButtonHeight
                    readonly property string text1: "中"
                    readonly property string text2: "En"
                    Text {
                        id: languageText1
                        anchors.verticalCenter: parent.verticalCenter
                        anchors.rightMargin: -10
                        anchors.right: parent.horizontalCenter
                        text: languageButton.text1
                        font.pixelSize: keyTextFontSize
                    }
                    Text {
                        id: languageText2
                        anchors.verticalCenter: parent.verticalCenter
                        anchors.left: parent.horizontalCenter
                        anchors.margins: 10
                        text: languageButton.text2
                        font.pixelSize: shiftKeyTextFontSize
                    }
                    states: [
                        State {
                            name: "English"
                            when: !chinese
                            PropertyChanges {
                                target: languageText1
                                text: languageButton.text2
                            }
                            PropertyChanges {
                                target: languageText2
                                text: languageButton.text1
                            }
                        }
                    ]
                    onClicked: {
                        chinese = !chinese
                        keySimulator.keyClick(Qt.Key_Space, Qt.ControlModifier, -1)
                    }

                }
                Loader {
                    id: commaButton
                    sourceComponent: keyButton
                    onLoaded: {
                        item.general_text = "，"
                        item.general_value = Qt.Key_Comma
                        item.shift_text = ","
                        item.shift_value = Qt.Key_Comma
                    }
                }
                ItemDelegate {
                    id: spaceButton
                    height: keyButtonHeight
                    width: keyButtonWidth * 3
                    Image {
                        height: parent.height
                        width: height
                        anchors.centerIn: parent
                        source: "icons/space.png"
                    }
                    onClicked: {
                        keySimulator.keyClick(Qt.Key_Space, Qt.NoModifier, -1)
                    }
                }
                Loader {
                    id: periodButton
                    sourceComponent: keyButton
                    onLoaded: {
                        item.general_text = "。"
                        item.general_value = Qt.Key_Period
                        item.shift_text = "."
                        item.shift_value = Qt.Key_Period
                    }
                }
                ItemDelegate {
                    id: putawayButton
                    width: keyButtonWidth
                    height: keyButtonHeight
                    Image {
                        height: parent.height
                        width: height
                        anchors.centerIn: parent
                        source: "icons/keyboard.png"
                    }
                    onClicked: {
                        rootDrop.start()
                        keySimulator.keyClick(Qt.Key_Escape, Qt.NoModifier, -1)
                        forceActiveFocus()
                    }
                }
                ItemDelegate {
                    id: tabButton
                    width: keyButtonWidth * 2
                    height: keyButtonHeight
                    Image {
                        height: parent.height
                        width: height
                        anchors.centerIn: parent
                        source: "icons/enter.png"
                    }
                    onClicked: {
                        keySimulator.keyClick(Qt.Key_Tab, Qt.NoModifier, -1)
                    }
                }
            }
        }
        Column {
            id: digitsKeyboard
            anchors.centerIn: parent
            spacing: keySpacing
            visible: false
            Row {
                anchors.horizontalCenter: parent.horizontalCenter
                spacing: keySpacing
                Repeater {
                    model: ListModel {
                        ListElement {key_text: "1"; key_value: Qt.Key_1}
                        ListElement {key_text: "2"; key_value: Qt.Key_2}
                        ListElement {key_text: "3"; key_value: Qt.Key_3}
                        ListElement {key_text: "4"; key_value: Qt.Key_4}
                        ListElement {key_text: "5"; key_value: Qt.Key_5}
                        ListElement {key_text: "6"; key_value: Qt.Key_6}
                        ListElement {key_text: "7"; key_value: Qt.Key_7}
                        ListElement {key_text: "8"; key_value: Qt.Key_8}
                        ListElement {key_text: "9"; key_value: Qt.Key_9}
                        ListElement {key_text: "0"; key_value: Qt.Key_0}
                    }
                    delegate: singleKeyButton
                }
            }
            Item {
                width: 1
                height: keyButtonHeight
            }

            Row {
                anchors.right: parent.right
                Loader {
                    id: digitsBackspaceButton
                    width: keyButtonWidth * 2
                    height: keyButtonHeight
                    sourceComponent: backspaceButton
                }
            }
            Row {
                anchors.right: parent.right
                ItemDelegate {
                    width: keyButtonWidth
                    height: keyButtonHeight
                    Text {
                        id: generalText
                        anchors.centerIn: parent
                        font.pixelSize: keyTextFontSize
                        text: "."
                    }
                    onClicked: {
                        keySimulator.keyClick(Qt.Key_Period, Qt.NoModifier, -1)
                    }

                }

                ItemDelegate {
                    id: digitsPutawayButton
                    width: keyButtonWidth
                    height: keyButtonHeight
                    Image {
                        height: parent.height
                        width: height
                        anchors.centerIn: parent
                        source: "icons/keyboard.png"
                    }
                    onClicked: {
                        rootDrop.start()
                        keySimulator.keyClick(Qt.Key_Escape, Qt.NoModifier, -1)
                        forceActiveFocus()
                    }
                }
                ItemDelegate {
                    id: digitsTabButton
                    width: keyButtonWidth * 2
                    height: keyButtonHeight
                    Image {
                        height: parent.height
                        width: height
                        anchors.centerIn: parent
                        source: "icons/enter.png"
                    }
                    onClicked: {
                        keySimulator.keyClick(Qt.Key_Tab, Qt.NoModifier, -1)
                    }
                }
            }
        }
    }
    TestEvent {
        id: keySimulator
    }

    Component {
        id: keyButton
        ItemDelegate {
            id: keyButtonRoot
            width: keyButtonWidth
            height: keyButtonHeight
            property string general_text: key_text
            property int general_value: key_value
            property string shift_text: shift_key_text
            property int shift_value: shift_key_value
            Text {
                id: generalText
                anchors.centerIn: parent
                font.pixelSize: keyTextFontSize
                text: general_text
            }
            Text {
                id: shiftText
                anchors.top: parent.top
                anchors.right: parent.right
                anchors.margins: 10
                text: shift_text
                font.pixelSize: shiftKeyTextFontSize
            }
            Connections {
                target: virtualKeyboardRoot
                onShiftChanged: {
                    var temp = keyButtonRoot.general_text
                    keyButtonRoot.general_text = keyButtonRoot.shift_text
                    keyButtonRoot.shift_text = temp
                    temp = keyButtonRoot.general_value
                    keyButtonRoot.general_value = keyButtonRoot.shift_value
                    keyButtonRoot.shift_value = temp
                }
            }
            onClicked: {
                //TestEvent仅使用Qt.ShiftModifier无法输入大写字母
                if (capsLock) {
                    helper.keyClickUpper(general_value);
                }
                else {
                    keySimulator.keyClick(general_value, Qt.NoModifier, -1)
                }
            }
            onPressAndHold: {
                keySimulator.keyClick(shift_value, Qt.NoModifier, -1)
            }
        }
    }
    Component {
        id: singleKeyButton
        ItemDelegate {
            id: singleKeyButtonRoot
            width: keyButtonWidth
            height: keyButtonHeight
            property string general_text: key_text
            property int general_value: key_value
            Text {
                id: generalText
                anchors.centerIn: parent
                font.pixelSize: keyTextFontSize
                text: general_text
            }
            onClicked: {
                keySimulator.keyClick(general_value, Qt.NoModifier, -1)
            }
        }
    }
    Component {
        id: backspaceButton
        ItemDelegate {
            id: backspaceButtonRoot
            width: keyButtonWidth * 2
            height: keyButtonHeight
            Image {
                anchors.centerIn: parent
                height: keyButtonHeight
                width: height
                source: "icons/backspace.png"
            }
            Timer {
                id: backspaceTimer
                repeat: true
                interval: 50
                onTriggered: {
                    keySimulator.keyClick(Qt.Key_Backspace, Qt.NoModifier, -1)
                }
            }
            onClicked: {
                keySimulator.keyClick(Qt.Key_Backspace, Qt.NoModifier, -1)
            }
            onPressAndHold: {
                backspaceTimer.start()
            }

            onReleased: {
                backspaceTimer.stop()
            }
        }
    }

    SequentialAnimation {
        id: rootRaise
        property int y: root.y
        PropertyAnimation { target: root; properties: "y"; to: rootRaise.y; duration: 200; easing.type: Easing.InOutQuad; }
    }
    SequentialAnimation {
        id: rootDrop
        PropertyAnimation { target: root; properties: "y"; to: defaultRootY; duration: 200; easing.type: Easing.InOutQuad; }
    }

    function call(activeFocusControl, offset) {
        shift = false
        //判断是否调出虚拟键盘
        if (Input.classIsListen(helper.getClassName(activeFocusControl), true)
                && !activeFocusControl.readOnly) {
            state = "visible"
            active = true
            //判断虚拟键盘遮挡
            if ((helper.getAbsolutePos(activeFocusControl).y - root.y + activeFocusControl.height)
                    > (height - keyboardHeight - offset)) {
                var absPos = helper.getAbsolutePos(activeFocusControl).y - root.y
                rootRaise.y = window.height - keyboardHeight
                        - absPos - activeFocusControl.height
                        - offset
                rootRaise.start()
                raise = true
                coverItem.height = window.height - (window.height + rootRaise.y + virtualKeyboard.item.keyboardHeight)
            }
            else {
                raise = false
                coverItem.height = 0
            }
            ///判断是否为仅数字
            if (activeFocusControl.inputMethodHints & Qt.ImhDigitsOnly) {
                state = "digits"
            }
        }
        else {
            if (raise) {
                rootDrop.start()
            }
            coverItem.height = 0
            raise = false
            active = false
            state = ""
        }
    }

    states: [
        State {
            name: "visible"
            PropertyChanges {
                target: virtualKeyboardRec
                y: virtualKeyboardRoot.height - virtualKeyboardRec.height
            }
        },
        State {
            name: "digits"
            extend: "visible"
            PropertyChanges { target: generalKeyboard; visible: false }
            PropertyChanges { target: digitsKeyboard; visible: true }
        }
    ]
    transitions: Transition {
        from: ""
        reversible: true
        ParallelAnimation {
            NumberAnimation {
                properties: "y"
                duration: 250
                easing.type: Easing.InOutQuad
            }
        }
    }
}
